package com.example.auth.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.auth.model.RoleMenuModel;
import com.example.auth.model.RoleModel;
import com.example.auth.service.RoleMenuModelService;
import com.example.db.DBSql;
import com.example.db.SqlBuilder;
import com.example.db.TableService;
import com.example.db.model.IPage;
import com.example.utils.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Classname RoleMenuModelServiceImpl
 * @Description TODO
 * @Date 2022/7/31 10:14
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class RoleMenuModelServiceImpl implements RoleMenuModelService {

    private static final String ROLE_MENU_TABLE=" select a.id, roId,menuId,rolename,title,roleno,a.createdate " +
            " from sys_role_menu a left join sys_role b ON a.roId=b.id " +
            " left join sys_menu c ON a.menuId=c.id ";

    @Autowired
    TableService tableService;

    @Autowired
    DBSql dbSql;

    @Override
    public IPage<RoleMenuModel> pageRoleMenuModel(int pageNum, int pageSize, JSONObject object) {
        return this.getRoleMenuModels(pageNum,pageSize,object);
    }

    @Override
    public void deleteRoleMenuByRoleIdandMenuId(Object RoleId, Object menuId) {
        String sql="delete from sys_role_menu where roId='%s' and menuId='%s'";
        sql=String.format(sql,RoleId,menuId);
        dbSql.update(sql);
    }

    @Override
    public RoleMenuModel saveRoleMenuModel(RoleMenuModel roleMenuModel) {
        tableService.save(roleMenuModel,RoleMenuModel.class);
        return roleMenuModel;
    }

    @Override
    public RoleMenuModel updateRoleMenuModel(RoleMenuModel roleMenuModel) {
        tableService.updateById(roleMenuModel,RoleMenuModel.class);
        return roleMenuModel;
    }

    @Override
    public RoleMenuModel getRoleModelById(Object id) {
       return this.getRoleMenuModelById(id);
    }


    @Override
    public RoleMenuModel getRoleMenuModelByRoleIdandMenuId(Object roleId, Object menuId) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(RoleMenuModel.class);
        sqlBuilder.and("roId").eq(roleId);
        sqlBuilder.and("menuId").eq(menuId);
        RoleMenuModel model=tableService.selectOne(sqlBuilder,RoleMenuModel.class);
        return model;
    }

    @Override
    public List<Integer> getRoleMenuModelMenuListByRoleId(Object roid) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(RoleMenuModel.class);
        sqlBuilder.and("roId").eq(roid);
        List<RoleMenuModel> roleMenuModel=tableService.selectList(sqlBuilder,RoleMenuModel.class);
        if(roleMenuModel==null || roleMenuModel.isEmpty()){
            return Collections.emptyList();
        }
        List<Integer> integerList=roleMenuModel.stream().map((e)->{
            return e.getMenuId();
        }).collect(Collectors.toList());
        return integerList;
    }


    @Override
    public List<Integer> getRoleMenuListRoleIds(List<Integer> roleIds) {
        if(roleIds==null || roleIds.isEmpty()){
            return Collections.emptyList();
        }
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(RoleMenuModel.class);
        String str=JSONArray.toJSONString(roleIds);
        List<String> strings=JSONArray.parseArray(str,String.class);
        sqlBuilder.and("roId").in(strings);
        List<RoleMenuModel> roleMenuModelList=tableService.selectList(sqlBuilder,RoleMenuModel.class);
        if(roleMenuModelList==null || roleMenuModelList.size() == 0){
            return Collections.emptyList();
        }
        List<Integer> menuIds=roleMenuModelList.stream().map((e)->{ return e.getMenuId(); }).collect(Collectors.toList());
        return menuIds;
    }

    /**
     * 搜索
     * @param object
     * @return
     */
    private IPage<RoleMenuModel> getRoleMenuModels(int pageNum, int pageSize, JSONObject object) {
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.select(" * ").from("("+ROLE_MENU_TABLE+") a ");
        if(object!=null){
            Object rolename=object.get("rolename");
            if(!StringUtils.isEmpty(rolename)){
                sqlBuilder.and("rolename").like(rolename);
            }
            Object roleno=object.get("roleno");
            if(!StringUtils.isEmpty(roleno)){
                sqlBuilder.and("roleno").like(roleno);
            }
            Object title=object.get("title");
            if (!StringUtils.isEmpty(title)) {
                sqlBuilder.and("title").like(title);
            }
        }
        sqlBuilder.page(pageNum,pageSize);
        sqlBuilder.orderByDesc("createdate");
        String pageSql=sqlBuilder.getPage();
        String counSql=sqlBuilder.getCountSql();
        List<RoleMenuModel> roleMenuModels=dbSql.selectList(pageSql,RoleMenuModel.class);
        int count=dbSql.selectCount(counSql);
        IPage<RoleMenuModel> roleModel=new IPage(count,roleMenuModels);
        return roleModel;
    }

    private RoleMenuModel getRoleMenuModelById(Object id){
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.select("*").from("("+ROLE_MENU_TABLE+") a ");
        sqlBuilder.or("id").eq(id);
        String sql=sqlBuilder.getSql();
        RoleMenuModel roleMenuModel=dbSql.selectMapObj(sql,RoleMenuModel.class);
        return roleMenuModel;
    }




}
